package com.coderman.controller.business;

import com.coderman.business.service.imp.GradeServiceImpl;
import com.coderman.common.annotation.ControllerEndpoint;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.student.Grade;
import com.coderman.common.response.ResponseBean;
import com.coderman.common.vo.student.GradeVO;
import com.coderman.common.vo.system.PageVO;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "年级管理")
@RestController
@RequestMapping("/grade")
/**
 * 咨询管理控制层
 * @auther 文晓平
 * @version 1.0
 * @date 2023-1-9
 */
public class GradeController {

    @Autowired
    private GradeServiceImpl gradeService;

    /**
     * 年级列表
     *
     * @return
     */
    @ApiOperation(value = "年级列表", notes = "模糊查询年级列表")
    @GetMapping("/findList")
    @ResponseBody
    public ResponseBean<PageVO<Grade>> findList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "7") Integer pageSize,
                                                     GradeVO gradeVO) {
        PageVO<Grade> gradeList = gradeService.findList(pageNum, pageSize, gradeVO);
        return ResponseBean.success(gradeList);
    }


    /**
     * 添加年级
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "添加年级失败", operation = "添加年级")
    @RequiresPermissions({"grade:add"})
    @ApiOperation(value = "添加年级")
    @PostMapping("/add")
    @ResponseBody
    public ResponseBean add(@RequestBody @Validated GradeVO gradeVO) {
        gradeService.add(gradeVO);
        return ResponseBean.success();
    }

    /**
     * 编辑年级信息
     *
     * @param id 编号
     * @return
     */
    @ApiOperation(value = "编辑年级")
    @RequiresPermissions({"grade:edit"})
    @GetMapping("/edit/{id}")
    @ResponseBody
    public ResponseBean edit(@PathVariable Integer id) throws SystemException {
        GradeVO gradeVO = gradeService.edit(id);
        return ResponseBean.success(gradeVO);
    }

    /**
     * 更新年级操作
     *
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "更新年级失败", operation = "更新年级")
    @ApiOperation(value = "更新年级")
    @RequiresPermissions({"grade:update"})
    @PutMapping("/update/{id}")
    @ResponseBody
    public ResponseBean update(@PathVariable Integer id, @RequestBody @Validated GradeVO gradeVO) throws SystemException {
        gradeService.update(id, gradeVO);
        return ResponseBean.success();
    }

    /**
     * 删除年级
     *
     * @param id
     * @return
     */
    @ControllerEndpoint(exceptionMessage = "删除年级失败", operation = "删除年级")
    @ApiOperation(value = "删除年级")
    @RequiresPermissions({"grade:delete"})
    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public ResponseBean delete(@PathVariable Integer id) throws SystemException {
        gradeService.delete(id);
        return ResponseBean.success();
    }




    /**
     * 导出excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有年级的excel表格")
    @PostMapping("/excel")
    @RequiresPermissions("grade:export")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出年级excel")
    public void export(HttpServletResponse response) {
        List<Grade> grades = this.gradeService.findAll();
        ExcelKit.$Export(Grade.class, response).downXlsx(grades, false);
    }

    /**
     * 所有年级
     *
     * @return
     */
    @ApiOperation(value = "所有年级")
    @GetMapping("/findAll")
    @ResponseBody
    public ResponseBean<List<GradeVO>> findAll() {
        List<GradeVO> gradeVOS = gradeService.findAllVO();
        return ResponseBean.success(gradeVOS);
    }


}
